/*
 * Created by: Peter Weissbrod
 * Created: Saturday, November 03, 2007
 */
using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Utils;

namespace EntityLayer.Entities
{
    /// <summary>
    /// this is a class heirarchy where all of the derived classes are stored within the same table
    /// the type of each class is determined by a discriminator value
    /// http://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
    /// </summary>
    [ActiveRecord("SalesItems", DiscriminatorColumn = "SalesItemType", DiscriminatorType = "String",DiscriminatorValue = "GenericSalesItem")]
    public abstract class SalesItem : ActiveRecordBase<SalesItem>
    {
        #region members
        private string name = Nulls.String;
        private int salesItemID = Nulls.ID;
        private User user = null;
        private decimal price = 0M;
        private DateTime postDate = Nulls.DateTime;
        private int year = 2000;
        private Enumerations.SalesItemStatusTypes salesItemStatusType = Enumerations.SalesItemStatusTypes.Null;
        private IList<Course> courses = new List<Course>();
        #endregion

        #region properties
        [PrimaryKey(PrimaryKeyType.Identity, "SalesItemID")]
        public int SalesItemID
        {
            get { return salesItemID; }
            set { salesItemID = value; }
        }
        [Property(NotNull = true)]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        [BelongsTo("UserID",NotNull = true)]
        public User User
        {
            get { return user; }
            set { user = value; }
        }
        [Property(NotNull = true)]
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }
        [Property(NotNull = true)]
        public DateTime PostDate
        {
            get { return postDate; }
            set { postDate = value; }
        }
        [Property(NotNull = true)]
        public int Year
        {
            get { return year; }
            set { year = value; }
        }
        [Property(NotNull = true)]
        public Enumerations.SalesItemStatusTypes SalesItemStatusType
        {
            get { return salesItemStatusType; }
            set { salesItemStatusType = value; }
        }
        [HasAndBelongsToMany(typeof(Course), Table = "SalesItemCourses", ColumnKey = "SalesItemID", ColumnRef = "CourseID", Lazy = true
            )]
        public IList<Course> Courses
        {
            get { return courses; }
            set { courses = value; }
        }
        #endregion

        #region methods
        protected SalesItem()
        {
        }
        protected SalesItem(string name, User user, decimal price, DateTime postDate, int year)
        {
            salesItemStatusType = Enumerations.SalesItemStatusTypes.Open;
            this.name = name;
            this.user = user;
            this.price = price;
            this.postDate = postDate;
            this.year = year;
        }
        #endregion

        public List<SalesItem> GetAll()
        {
            throw new NotImplementedException();
        }
        public void SaveOrUpdate(SalesItem t)
        {
            t.SaveAndFlush();
        }
        public void Delete(SalesItem t)
        {
            t.DeleteAndFlush();
        }
    }
}